package service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import database.Conn;
// Class thêm, sửa xóa việc đặt phòng
public class Booking {
	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblBooking";
	
	public boolean AddBooking(int id_room, int id_customer, int check_in,int check_out,int status){
		//Hàm thêm booking mới
		connect = Conn.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+this.table+"(id_room,id_customer,check_in,check_out,status) VALUES (?,?,?,?,?)");
			pstm.setInt(1,id_room);
			pstm.setInt(2,id_customer);
			pstm.setInt(3,check_in);
			pstm.setInt(4,check_out);
			pstm.setInt(5,status);
			if(pstm.executeUpdate()>0){
				return true;
			}
	
		}catch(SQLException e){
			
		}
		return false;
		
	}
	public boolean DeleteBooking(int id){
		connect = Conn.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean UpdateBooking(int id,int id_room, int id_customer, int check_in,int check_out,int status){
		connect= Conn.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +"SET id_room=?,id_customer=?,check_in=?," +
					"check_out=?,status=?  WHERE id=?");
			pstm.setInt(1,id_room );
			pstm.setInt(2,id_customer);
			pstm.setInt(3,check_in);
			pstm.setInt(4,check_out);
			pstm.setInt(5,status);
			pstm.setInt(6,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.Booking> GetAllBooking(){
		connect= Conn.getConnect();
		ResultSet rs=null;
		ArrayList<entity.Booking> list = new ArrayList<entity.Booking>();
		try{

			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
			while(rs.next()){
			entity.Booking booking = new entity.Booking();
			booking.setId_booking(rs.getInt("id"));
			booking.setId_customer(rs.getInt("id_customer"));
			booking.setId_room(rs.getInt("id_room"));
			booking.setCheck_in(rs.getInt("check_in"));
			booking.setCheck_out(rs.getInt("check_out"));
		
			list.add(booking);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return list;
	}
	public void GetBookingById(int id){
		entity.Booking booking =new entity.Booking();
		connect= Conn.getConnect();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
			while(rs.next()){
				booking.setId_customer(rs.getInt("id_customer"));
				booking.setId_room(rs.getInt("id_room"));
				booking.setCheck_in(rs.getInt("check_in"));
				booking.setCheck_out(rs.getInt("check_out"));
			}
	
			// Lay thong tin gui den Entity
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
        
        
	
}
